package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.ActivityTypeMapper;
import com.hbnu.system.model.entity.ActivityType;
import com.hbnu.system.service.ActivityTypeService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ActivityTypeServiceImpl extends ServiceImpl<ActivityTypeMapper, ActivityType> implements ActivityTypeService {
    @Override
    public boolean addActivityType(ActivityType activityType) {
        ActivityType activityType1 = super.getOne(Wrappers.lambdaQuery(ActivityType.class)
                .eq(ActivityType::getActivityTypeName,activityType.getActivityTypeName()));
        if(StrUtil.isEmpty(activityType.getActivityTypeName())||StrUtil.isBlank(activityType.getActivityTypeName())){
            throw new BaseException("该活动类型名称为空");
        }
        if(!Objects.isNull(activityType1)){
            throw new BaseException("该活动类型名称已存在");
        }
        return baseMapper.insert(activityType)==1;
    }

    @Override
    public List<ActivityType> getActivityTypeList() {
        List<ActivityType> depts = baseMapper.selectList(Wrappers.lambdaQuery(ActivityType.class));
//        List<String> result = new ArrayList<>();
//        for (Dept dept : depts) {
//            result.add(dept.getDeptName());
//        }
        return depts;
    }

    @Override
    public boolean updateActivityType(ActivityType projectcate) {
        if(StrUtil.isEmpty(projectcate.getActivityTypeName())||StrUtil.isBlank(projectcate.getActivityTypeName())){
            throw new BaseException("该活动类型名称为空");
        }
        if(baseMapper.updateById(projectcate)==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteActivityType(String ID) {
        return baseMapper.deleteById(ID) == 1;
    }

    @Override
    public List<ActivityType> getActivityTypeListByName(String activityTypeName) {
        LambdaQueryWrapper<ActivityType> queryWrapper = Wrappers.lambdaQuery(ActivityType.class);

        // 如果 activityTypeName 不为空且不是空白字符串，则添加模糊匹配条件
        if (StringUtils.isNotBlank(activityTypeName)) {
            queryWrapper.like(ActivityType::getActivityTypeName, activityTypeName);
        }

        return baseMapper.selectList(queryWrapper);
    }
}
